{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12345\n"
     ]
    }
   ],
   "source": [
    "str_sample = '12345'\n",
    "int_sample = int(str_sample)\n",
    "print(int_sample)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "invalid literal for int() with base 10: 'A'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-2-df3ce26a1a57>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0mstr_sample\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'A'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mint_sample\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mstr_sample\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      3\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mint_sample\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mValueError\u001b[0m: invalid literal for int() with base 10: 'A'"
     ]
    }
   ],
   "source": [
    "str_sample = 'A'\n",
    "int_sample = int(str_sample)\n",
    "print(int_sample)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on class Exception in module builtins:\n",
      "\n",
      "class Exception(BaseException)\n",
      " |  Common base class for all non-exit exceptions.\n",
      " |  \n",
      " |  Method resolution order:\n",
      " |      Exception\n",
      " |      BaseException\n",
      " |      object\n",
      " |  \n",
      " |  Methods defined here:\n",
      " |  \n",
      " |  __init__(self, /, *args, **kwargs)\n",
      " |      Initialize self.  See help(type(self)) for accurate signature.\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Static methods defined here:\n",
      " |  \n",
      " |  __new__(*args, **kwargs) from builtins.type\n",
      " |      Create and return a new object.  See help(type) for accurate signature.\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Methods inherited from BaseException:\n",
      " |  \n",
      " |  __delattr__(self, name, /)\n",
      " |      Implement delattr(self, name).\n",
      " |  \n",
      " |  __getattribute__(self, name, /)\n",
      " |      Return getattr(self, name).\n",
      " |  \n",
      " |  __reduce__(...)\n",
      " |      Helper for pickle.\n",
      " |  \n",
      " |  __repr__(self, /)\n",
      " |      Return repr(self).\n",
      " |  \n",
      " |  __setattr__(self, name, value, /)\n",
      " |      Implement setattr(self, name, value).\n",
      " |  \n",
      " |  __setstate__(...)\n",
      " |  \n",
      " |  __str__(self, /)\n",
      " |      Return str(self).\n",
      " |  \n",
      " |  with_traceback(...)\n",
      " |      Exception.with_traceback(tb) --\n",
      " |      set self.__traceback__ to tb and return self.\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Data descriptors inherited from BaseException:\n",
      " |  \n",
      " |  __cause__\n",
      " |      exception cause\n",
      " |  \n",
      " |  __context__\n",
      " |      exception context\n",
      " |  \n",
      " |  __dict__\n",
      " |  \n",
      " |  __suppress_context__\n",
      " |  \n",
      " |  __traceback__\n",
      " |  \n",
      " |  args\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(Exception)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['ArithmeticError', 'AssertionError', 'AttributeError', 'BlockingIOError', 'BrokenPipeError', 'BufferError', 'ChildProcessError', 'ConnectionAbortedError', 'ConnectionError', 'ConnectionRefusedError', 'ConnectionResetError', 'EOFError', 'EnvironmentError', 'FileExistsError', 'FileNotFoundError', 'FloatingPointError', 'IOError', 'ImportError', 'IndentationError', 'IndexError', 'InterruptedError', 'IsADirectoryError', 'KeyError', 'LookupError', 'MemoryError', 'ModuleNotFoundError', 'NameError', 'NotADirectoryError', 'NotImplementedError', 'OSError', 'OverflowError', 'PermissionError', 'ProcessLookupError', 'RecursionError', 'ReferenceError', 'RuntimeError', 'SyntaxError', 'SystemError', 'TabError', 'TimeoutError', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'ValueError', 'WindowsError', 'ZeroDivisionError']\n"
     ]
    }
   ],
   "source": [
    "print([i for i in dir(__builtin__) if 'Error' in i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "invalid literal for int() with base 10: 'A'\n"
     ]
    }
   ],
   "source": [
    "str_sample = 'A'\n",
    "try:\n",
    "    int_sample = int(str_sample)\n",
    "except ValueError as err:\n",
    "    print(err)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "这是else的代码：1有效\n",
      "这是finally的代码：不管有没有效，都要来凑个热闹\n",
      "这是except的代码：A无效\n",
      "这是finally的代码：不管有没有效，都要来凑个热闹\n",
      "这是else的代码：2有效\n",
      "这是finally的代码：不管有没有效，都要来凑个热闹\n",
      "这是except的代码：B无效\n",
      "这是finally的代码：不管有没有效，都要来凑个热闹\n",
      "这是else的代码：3有效\n",
      "这是finally的代码：不管有没有效，都要来凑个热闹\n",
      "这是except的代码：C无效\n",
      "这是finally的代码：不管有没有效，都要来凑个热闹\n"
     ]
    }
   ],
   "source": [
    "for i in ['1','A','2','B','3','C']:\n",
    "    try:\n",
    "        int(i)\n",
    "    except Exception:\n",
    "        print(f'这是except的代码：{i}无效')\n",
    "    else:\n",
    "        print(f'这是else的代码：{i}有效')\n",
    "    finally:\n",
    "        print('这是finally的代码：不管有没有效，都要来凑个热闹')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1有效\n",
      "不管有没有效，都要来凑个热闹\n",
      "2有效\n",
      "不管有没有效，都要来凑个热闹\n",
      "这时的数是20 数字已经超出范围\n",
      "不管有没有效，都要来凑个热闹\n",
      "3有效\n",
      "不管有没有效，都要来凑个热闹\n",
      "这时的数是30 数字已经超出范围\n",
      "不管有没有效，都要来凑个热闹\n",
      "4有效\n",
      "不管有没有效，都要来凑个热闹\n"
     ]
    }
   ],
   "source": [
    "class MyError(Exception):\n",
    "    \n",
    "    def __init__(self,errstr):\n",
    "        super().__init__(errstr)\n",
    "\n",
    "def which_bigger(num):\n",
    "    if num > 10:\n",
    "        raise MyError('数字已经超出范围')\n",
    "    else:\n",
    "        return num\n",
    "        \n",
    "          \n",
    "for i in [1,2,20,3,30,4]:\n",
    "    try:\n",
    "        which_bigger(i)\n",
    "    except MyError as err:\n",
    "        print(f'这时的数是{i}',err)\n",
    "    else:\n",
    "        print(f'{i}有效')\n",
    "    finally:\n",
    "        print('不管有没有效，都要来凑个热闹')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 225,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<method-wrapper '__ge__' of WestTeam object at 0x03C45A10>\n"
     ]
    }
   ],
   "source": [
    "class WestTeam(object):\n",
    "    \n",
    "    \"\"\"说明：这是关于取经小队的类\"\"\"\n",
    "    \n",
    "    def __init__(self,name,weapon):\n",
    "        self.name = name\n",
    "        self.weapon = weapon\n",
    "        \n",
    "    def weapon_use(self):\n",
    "        print(f'{self.name}的兵器是{self.weappon}')\n",
    "        \n",
    "    def show_size(self):\n",
    "        print(f'{self.name} 占用的空间为 {self.__sizeof__()}')\n",
    "        \n",
    "        \n",
    "wukong = WestTeam('悟空','金箍棒')       \n",
    "print(wukong.__ge__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 208,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function repr in module builtins:\n",
      "\n",
      "repr(obj, /)\n",
      "    Return the canonical string representation of the object.\n",
      "    \n",
      "    For many object types, including most builtins, eval(repr(obj)) == obj.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(repr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "['__delattr__', '__dict__','__eq__', '__format__', '__ge__', '__getattribute__', \n",
    " '__gt__', '__hash__','__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
